package com.study.thread.juc.lock;


public class Semaphore1Demo {
    private static Semaphore1 semaphone = new Semaphore1();

    public static void main(String[] args) {
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "：开始执行");
                System.out.println(Thread.currentThread().getName() + "：结束");
            } finally {
                semaphone.release();
                System.out.println(Thread.currentThread().getName() + "：释放");
            }
        }).start();

        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "：开始执行");
                System.out.println(Thread.currentThread().getName() + "：结束");
            } finally {
                semaphone.release();
                System.out.println(Thread.currentThread().getName() + "：释放");
            }
        }).start();

        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "：开始执行");
                System.out.println(Thread.currentThread().getName() + "：结束");
            } finally {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                semaphone.release();
                System.out.println(Thread.currentThread().getName() + "：释放");
            }
        }).start();
        System.out.println("主线程等待----");
        semaphone.acquire(3);
        System.out.println("主线程释放----");

    }


}
